#pragma once 


#include "NirTwoVector.h"
#include "NirPair.h"

//namespace my {
//
	template <class K, class V>
	class Map {
		Vector<Pair<K, V> >* data;            // array of pointers

	public:
		typedef Pair<K, V>* iterator;

		Map()
		{
			data = new Vector<Pair<K, V> >();
		}

		~Map()
		{
			// delete data;
		}

		Map(const Map<K, V>& other) : data(other.data) {} // user-defined copy ctor

		Map& operator= (const Map& other)
		{
			data = new Vector<Pair<K, V> >(*other.data);
			return *this;
		}

		V& operator[] (const K& key)
		{
			typename Vector<Pair<K, V> >::iterator iter = data->begin();
			for (iter = data->begin(); iter != data->end(); ++iter)
			{
				//if (*iter->first == key)
				//{
				//	break;
				//}
				if (iter->first == key)
				{
					break;
				}
			}

			if (iter == data->end()) // not found key
			{
				//V* nullValue = new V();
				//Pair<K, V>* p = new Pair<K, V>(new K(key), nullValue);
				//data->push_back(*p);
				//return *(p->getValue());

				V* nullValue = new V();
				Pair<K, V>* p = new Pair<K, V>(key, *nullValue);
				data->push_back(*p);
				iterator endIter = this->end();
				endIter--;
				//V value = 
				return endIter->second;
			}
			else
			{
				//return *iter->second;
				return iter->second;
			}
		}

		V& at(const K& key)
		{
			typename Vector< Pair<K, V> >::iterator iter = data->begin();
			for (iter = data->begin(); iter != data->end(); ++iter)
			{
				if (iter->first == key)
				{
					break;
				}
				//if (*iter->first == key)
				//{
				//	break;
				//}
			}

			if (iter == data->end()) // not found key
			{
				throw "map.at: index out of range";
			}
			else
			{
				//return *iter->second;
				return iter->second;
			}
		}

		void clear()
		{
			data->clear();
		}

		void insert(const Pair<K, V>& pair)
		{
			data->push_back(pair);
		}

		iterator begin() { return data->begin(); }

		iterator end()  { return data->end(); }

		iterator find(const K& key)
		{
			typename Vector< Pair<K, V> >::iterator iter = data->begin();
			for (iter = data->begin(); iter != data->end(); ++iter)
			{
				//if (*iter->first == key)
				//{
				//	break;
				//}
				if (iter->first == key)
				{
					break;
				}
			}
			return iter;
		}

		void traverse()
		{
			iterator result = begin();
			for (; result != end(); ++result)
			{
			}
		}


	}; // class



//} // namespace
